package com.yonyou.pmclouds.milestone.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.consts.EnableStatusConst;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.basecom.runtime.RuntimeEnvironment;
import com.yonyou.pmclouds.milestone.entity.MilestoneDTO;
import com.yonyou.pmclouds.milestone.entity.MilestoneRefVO;
import com.yonyou.pmclouds.milestone.entity.MilestoneVO;
import com.yonyou.pmclouds.milestone.service.IMilestoneMaintain;
import com.yonyou.pmclouds.milestone.service.IMilestoneQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author th
 * @date 2019/11/6 17:39
 */
@RestController
@RequestMapping(value = "/milestone")
public class MilestoneController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IMilestoneQuery query;
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private IMilestoneMaintain maintain;

    /**
     * 列表查询，支持编码、名称模糊搜索
     *
     * @param enableStatus
     * @returnr
     * @throws BusinessException
     */
    @RequestMapping(value = "/list")
    public MilestoneVO[] queryByTenant(@RequestParam(value = "enableStatus", required = false) Integer enableStatus,
                                       @RequestParam(value = "keyword", required = false) String keyword) throws BusinessException {

        MilestoneVO[] result = query.queryByTenant(RuntimeEnvironment.getTenantId(),
                enableStatus == null ? null : enableStatus.byteValue(), keyword);
        return result;
    }

    /**
     * 里程碑档案参照查询，供其他单据引用
     *
     * @param pkTenant
     * @param keyword
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/refQuery")
    public MilestoneRefVO[] queryRefVOByTenant(@RequestParam(value = "pkTenant", required = false) String pkTenant,
                                               @RequestParam(value = "keyword", required = false) String keyword) throws BusinessException {
        if (StringUtils.isEmpty(pkTenant))
            pkTenant = RuntimeEnvironment.getTenantId();

        MilestoneRefVO[] result = query.queryRefByTenant(pkTenant, keyword);
        return result;
    }

    /**
     * 校验里程碑是否被引用
     *
     * @param pk
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/referenceCheck")
    public Boolean referenceCheck(@RequestParam("pk") String pk) throws BusinessException {
        return query.referenceCheck(pk);
    }

    /**
     * 保存
     *
     * @param milestoneDTO
     * @return
     * @throws BusinessException
     */
    @RequestMapping(value = "/save")
    public MilestoneVO[] save(@RequestBody MilestoneDTO milestoneDTO) throws BusinessException {
        MilestoneVO[] result = maintain.save(milestoneDTO.getVos(), milestoneDTO.getDeleteList());
        return result;
    }

    @RequestMapping(value = "/delete")
    public MilestoneVO delete(@RequestParam("pk") String pk) throws BusinessException {
        MilestoneVO resultVO = maintain.delete(pk);
        return resultVO;
    }

    @RequestMapping(value = "/enable")
    public MilestoneVO enable(@RequestBody MilestoneVO vo) throws BusinessException {
        vo.setEnableStatus(EnableStatusConst.ENABLE);
        MilestoneVO resultVO = maintain.updateEnableStatus(vo);
        return resultVO;
    }

    @RequestMapping(value = "/disable")
    public MilestoneVO disable(@RequestBody MilestoneVO vo) throws BusinessException {
        vo.setEnableStatus(EnableStatusConst.DISABLE);
        MilestoneVO resultVO = maintain.updateEnableStatus(vo);
        return resultVO;
    }
}
